package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.util.ImageDownload;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @author linxz
 * @since 2022-12-27 08:18
 */
@RestController
@RequestMapping("/fileManager")
public class FileController {

    // 修改：1.上传了的文件存储目录 2. 返回图片url怎么命名

    //表单上传
    @PostMapping(value = "/upLoadFile")
    public AjaxResult upLoadFile(@RequestParam("file") MultipartFile file) {
        HashMap<String, Object> result = ImageDownload.getInstance(ImageDownload.SAVE_PATH).uploadByFile(file);
        String fileUrl = result.get("fileUrl").toString();
        //构建返回实体
        // fileUrl -> image\1717893445591.jpg
        //  fileUrl ->  fileUrl -> mp4\1718505936892.mp4
        // mp4/1717257886169.mp4
        String path=fileUrl.replace('\\','/');
        return AjaxResult.success("操作成功", path);

    }


//    @ApiOperation(value = "Base64文件上传")
//    @PostMapping(value = "/uploadBase64")
//    //@OpenApiAuth
//    public CommonResult<SysFile> uploadBase64(@RequestBody Base64Params base64Param){
//        if(StringUtils.isEmpty(base64Param.getBase64())){
//           throw new ApiException("图片长度不能为空");
//        }
//        if (StringUtils.isEmpty(base64Param.getType())){
//            throw new ApiException("业务类型不能为空");
//        }
//        HashMap<String, Object> result = ImageDownload.getInstance(wxMaProperties.getFileSavePath()).uploadByBase64(base64Param.getBase64(),base64Param.getType());
//        String fileName = result.get("fileName").toString();
//        String filePath = result.get("filePath").toString();
//        String fileUrl = result.get("fileUrl").toString();
//        Long fileSize = Long.parseLong(result.get("fileSize").toString());
//        SysFile sysFile = SysFile.builder()
//                .businessType(base64Param.getBusinessType())
//                .businessName(base64Param.getBusinessName())
//                .businessId(base64Param.getBusinessId())
//                .fileName(fileName)
//                .filePath(filePath)
//                .fileUrl(fileUrl)
//                .fileSize(fileSize)
//                .creator(base64Param.getCreator())
//                .build();
//        sysFile = iSysFileService.add(sysFile);
//        return CommonResult.success(sysFile,"上传成功");
//    }


}
